package com.whfc.ms.api.hdc.controller;

import com.whfc.common.result.ListData;
import com.whfc.common.result.PageData;
import com.whfc.common.result.Result;
import com.whfc.common.result.ResultUtil;
import com.whfc.common.util.JSONUtil;
import com.whfc.common.util.SessionAttr;
import com.whfc.fuum.entity.SysUser;
import com.whfc.fuum.param.UpgradeDeviceParam;
import com.whfc.hdc.dto.AppDeviceUpgradeDTO;
import com.whfc.hdc.dto.AppDeviceUpgradePackageDTO;
import com.whfc.ms.api.hdc.service.MsUpgradeService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * @Description: 硬件升级
 * @author: xugcheng
 * @version: 1.0
 * @date: 2020-12-25 14:40
 */
@RequiresPermissions("hdc/upgrade")
@RestController
@RequestMapping("/ms/api/hdc/upgrade")
public class AppDeviceUpgradeController {

    private Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private MsUpgradeService msUpgradeService;

    @GetMapping("package/list")
    public Result upgradePackageList(@SessionAttribute(SessionAttr.MS_USER) SysUser loginUser,
                                     @RequestParam("pageNum") Integer pageNum,
                                     @RequestParam("pageSize") Integer pageSize,
                                     @RequestParam(value = "deviceType", required = false) Integer deviceType) {
        logger.info("查询升级包列表,loginUser:{},deviceType:{},pageNum:{},pageSize:{}", loginUser.getUsername(), deviceType, pageNum, pageSize);
        PageData<AppDeviceUpgradePackageDTO> pageData = msUpgradeService.upgradePackageList(deviceType, pageNum, pageSize);
        return ResultUtil.success(pageData);
    }

    @PostMapping("package/add")
    public Result addUpgradePackge(@SessionAttribute(SessionAttr.MS_USER) SysUser loginUser,
                                   @RequestParam(value = "deviceType") Integer deviceType,
                                   @RequestParam(value = "masterVer", required = false) String masterVer,
                                   @RequestParam(value = "slaveVer", required = false) String slaveVer,
                                   @RequestParam(value = "file") MultipartFile file) {
        logger.info("上传升级包,loginUser:{},deviceType:{},masterVer:{},slaveVer:{},file:{}", loginUser.getUsername(), deviceType, masterVer, slaveVer, file.getOriginalFilename());
        msUpgradeService.addUpgradePackage(deviceType, masterVer, slaveVer, file);
        return ResultUtil.success();
    }

    @PostMapping("package/del/{packageId}")
    public Result delUpgradePackge(@SessionAttribute(SessionAttr.MS_USER) SysUser loginUser,
                                   @PathVariable("packageId") Integer packageId) {
        logger.info("删除升级包,loginUser:{},packageId:{}", loginUser.getUsername(), packageId);
        msUpgradeService.delUpgradePackage(packageId);
        return ResultUtil.success();
    }

    @GetMapping(value = "device/list", params = {"pageNum"})
    public Result upgradeDeviceList(@SessionAttribute(SessionAttr.MS_USER) SysUser loginUser,
                                    @RequestParam("pageNum") Integer pageNum,
                                    @RequestParam("pageSize") Integer pageSize,
                                    @RequestParam("packageId") Integer packageId,
                                    @RequestParam("state") Integer state,
                                    @RequestParam(value = "keyword", required = false) String keyword) {
        logger.info("查询升级硬件列表,loginUser:{},packageId:{},state:{},pageNum:{},pageSize:{},keyword:{}", loginUser.getUsername(), packageId, state, pageNum, pageSize, keyword);
        PageData<AppDeviceUpgradeDTO> pageData = msUpgradeService.upgradeDeviceList(pageNum, pageSize, packageId, state, keyword);
        return ResultUtil.success(pageData);
    }

    @GetMapping("device/list")
    public Result upgradeDeviceListAll(@SessionAttribute(SessionAttr.MS_USER) SysUser loginUser,
                                       @RequestParam("packageId") Integer packageId) {
        logger.info("查询升级硬件列表,loginUser:{},packageId:{}", loginUser.getUsername(), packageId);
        ListData<AppDeviceUpgradeDTO> list = msUpgradeService.upgradeDeviceList(packageId);
        return ResultUtil.success(list);
    }

    @PostMapping("device/add")
    public Result addUpgradeDevice(@SessionAttribute(SessionAttr.MS_USER) SysUser loginUser,
                                   @RequestBody UpgradeDeviceParam param) {
        logger.info("添加升级硬件,loginUser:{},body:{}", loginUser.getUsername(), JSONUtil.toString(param));
        Integer packageId = param.getPackageId();
        List<Integer> deviceIdList = param.getDeviceIdList();
        msUpgradeService.addUpgradeDevice(packageId, deviceIdList);
        return ResultUtil.success();
    }

    @PostMapping("device/del/{upgradeId}")
    public Result delUpgradeDevice(@SessionAttribute(SessionAttr.MS_USER) SysUser loginUser,
                                   @PathVariable("upgradeId") Integer upgradeId) {
        logger.info("删除升级硬件,loginUser:{},upgradeId:{}", loginUser.getUsername(), upgradeId);
        msUpgradeService.delUpgradeDevice(upgradeId);
        return ResultUtil.success();
    }
}
